package com.ffms.servlet;

import java.io.IOException;


import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ffms.entity.FamilyMember;
import com.ffms.service.AdminService;
import com.ffms.service.impl.AdminServiceImpl;

@WebServlet("/AddFMServlet")
public class AddFMServlet extends HttpServlet {


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //1.接收视图层传来的表单数据并封装完成对象
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        FamilyMember fm = new FamilyMember(name, pwd);
        //2。 调用业务层完成添加操作
        AdminService adminService = new AdminServiceImpl();
        int n = adminService.addFm(fm);
        //根据结果跳转到不同的页面
        if (n > 0) {
//			成功跳转到全部成员页面
            request.getRequestDispatcher("/ShowAllFMServlet").forward(request, response);
        } else {
//			失败则跳转到原页面
            request.setAttribute("error", "添加成员失败");
            request.getRequestDispatcher("/admin/familyMemberAdd.jsp").forward(request, response);
        }
    }

}
